﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using TBR.Repositories;
using TBR.Entities;
using System.Windows.Media.Imaging;
using TBR.Resources;

namespace TBR
{
    public partial class MainProfilePage : PhoneApplicationPage
    {
        #region members
        ProfileRepository profileRepository { get; set; }
        AccountRepository accountRepository { get; set; }
        UserDetails profile = null;
        #endregion

        #region constructor
        public MainProfilePage()
        {
            InitializeComponent();
            accountRepository = new AccountRepository();
            profileRepository = new ProfileRepository();
            accountRepository.AuthorizeCompleted += accountRepository_AuthorizeCompleted;
            profileRepository.GetProfilePageCompleted += profileRepository_GetProfilePageCompleted;
            accountRepository.Authorize("romandrovich", "20081990");//your credentials
        }
        #endregion

        #region private methods
        private void p_Main_Loaded(object sender, RoutedEventArgs e)
        {

        }
        private void accountRepository_AuthorizeCompleted(object source, AccountRepository.RequestEventArgs args)
        {
            profileRepository.GetProfile();
        }
        private void profileRepository_GetProfilePageCompleted(object source, ProfileRepository.RequestEventArgs args)
        {
            profile = args.Profile;
            LoadProfile();
        }
        private void LoadProfile()
        {
            t_Name.Text = String.Format("{0}, {1}", profile.Name, profile.Age);
            t_State.Text = profile.Details.State;
            i_Avatar.Source = new BitmapImage(new Uri(profile.ImgUrl, UriKind.RelativeOrAbsolute));
            //t_AboutMyself.Text = String.Format(AppResources.txtFrmtAboutMe, profile.Details.AboutMyself);

            i_t_Name.Text = String.Format(profile.Name);
            i_t_Age.Text = String.Format(AppResources.txtFrmtAge, profile.Age);
            i_t_State.Text = String.Format(AppResources.txtFrmtState, profile.Details.State);
            i_i_Avatar.Source = new BitmapImage(new Uri(profile.ImgUrl, UriKind.RelativeOrAbsolute));
            i_t_AboutMe.Text = String.Format(AppResources.txtFrmtAboutMe, profile.Details.AboutMyself);
            i_t_Sex.Text = String.Format(AppResources.txtFrmtSex, profile.Sex);
            i_t_SexualOrientation.Text = String.Format(AppResources.txtFrmtSexualOrientation, profile.Details.Orientation);
            i_t_Search.Text = String.Format(AppResources.txtFrmtSearch, profile.Details.Search);
            i_t_inAge.Text = String.Format(AppResources.txtFrmtInAge, profile.Details.InAge);
            i_t_Mission.Text = String.Format(AppResources.txtFrmtMission, profile.Details.Mission);
        }

        private void ChangeState_Click(object sender, RoutedEventArgs e)
        {
            t_State.IsEnabled = true;
            t_State.SelectAll();
            t_State.Focus();
        }

        private void t_State_LostFocus(object sender, RoutedEventArgs e)
        {
            t_State.IsEnabled = false;
            if (t_State.Text != profile.Details.State)
            {
                profileRepository.SetState(t_State.Text);
                i_t_State.Text = String.Format(AppResources.txtFrmtState, t_State.Text);
            }
        }

        private void bt_Friends_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/PeoplePage.xaml?page=Friends", UriKind.Relative));
        }
        private void bt_Guests_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/PeoplePage.xaml?page=Guests", UriKind.Relative));
        }

        private void bt_Messages_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/PanoramaMessages.xaml?page=Messages", UriKind.Relative));
        }
        #endregion
    }

}